﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.BasicService;
using RN.ERP.Web.EnterpriseService;
using RN.ERP.Web.ModelService;
using RN.ERP.Web.PersonService;
using Newtonsoft.Json;
using System.Web.Services;
using Fly.Common.Linq;

namespace RN.ERP.Web.Expert
{
    public partial class RegisterExpertGLD : System.Web.UI.Page
    {
        #region Params
        
        private BasicServiceClient basicService = new BasicServiceClient();
        private EnterpriseServiceClient enterpriseService = new EnterpriseServiceClient();
        private PersonServiceClient psnService = new PersonServiceClient();
        public override void VerifyRenderingInServerForm(Control control){}
        
        #endregion
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                if (Session[SessionConstant.SESSION_EXPERT_MOBILE] == null)
                {
                    Response.Redirect("ExpertOldEmpVerify.aspx");
                    return;
                }
                string expertTel = Session[SessionConstant.SESSION_EXPERT_MOBILE].ToString();
                string expertGLDStr = psnService.GetExpertGLDInfo(expertTel);
                hidExpertGLD.Value = expertGLDStr;

                tb_Emp_ExpertGLD expGLD = JsonConvert.DeserializeObject<tb_Emp_ExpertGLD>(expertGLDStr);
                if (expGLD != null && !String.IsNullOrEmpty(expGLD.Tel))
                {
                    ////根据编号获取名称
                    lblDBType.Text = basicService.GetExpertDBName(expGLD.AreaCode);
                    ucDbExp.DBCode = expGLD.AreaCode;
                }
                BindCorpUserType();
            }
        }

        /// <summary>
        /// 绑定企业类型
        /// </summary>
        private void BindCorpUserType()
        {
            ////获取所有企业类型
            Dictionary<string, string> dicCorpUserType = GetDdlByDic("CorpUserType", "");
            DDLCorpUserType_Expert.DataSource = dicCorpUserType;
            this.DDLCorpUserType_Expert.DataValueField = "Key";
            this.DDLCorpUserType_Expert.DataTextField = "Value";
            DDLCorpUserType_Expert.DataBind();
            DDLCorpUserType_Expert.Items.Insert(0, new ListItem("--请选择--", ""));
        }

        private Dictionary<string, string> GetDdlByDic(string type, string condition)
        {
            ////string result = Application[ApplicationConstant.APPLICATION_CORPUSERTYPE] == null ? string.Empty : Application[ApplicationConstant.APPLICATION_CORPUSERTYPE].ToString();
            ////if (string.IsNullOrEmpty(result))
            ////{
            ////    return new Dictionary<string, string>();
            ////}
            ////return JsonConvert.DeserializeObject<Dictionary<string, string>>(result);
            Dictionary<string,string> dic = new Dictionary<string, string>();
            dic.Add("15", "施工类企业");
            dic.Add("42", "勘察、设计类企业");
            dic.Add("45", "监理、造价咨询、招标代理等服务类企业");
            dic.Add("46", "材料生产、供应类企业");
            dic.Add("47", "设备生产、供应类企业");
            dic.Add("48", "其他");
            return dic;
        }

        #region Ajax

        [WebMethod]
        public static string VerifyExpertReg(string exMobile, string idCard)
        {
            int retMobileNum = 0;
            int retIdCard = 0;
            ////验证手机号
            using (PersonServiceClient personServiceClient = new PersonServiceClient())
            {
                QueryPersonInfoCondition condition = new QueryPersonInfoCondition();
                condition.MobileTel = new ParameterValue<string>() { V = exMobile, O = OptEnum.Equal };
                condition.ClassId = new ParameterValue<int?>() { V = 8065, O = OptEnum.Equal };
                List<QueryPersonInfoCondition> list = new List<QueryPersonInfoCondition>();
                list.Add(condition);
                int empNum = personServiceClient.GetExpertsCount(JsonConvert.SerializeObject(list), string.Empty);
                if (empNum > 0)
                {
                    retMobileNum = -1;
                }
                else
                {
                    retMobileNum = 1;
                }
            }

            ////验证专家身份证号码
            bool verifyIdCard = false;
            using (PersonServiceClient personServiceClient = new PersonServiceClient())
            {
                QueryPersonInfoCondition condition = new QueryPersonInfoCondition();
                condition.IDCard = new ParameterValue<string>() { V = idCard, O = OptEnum.Equal };
                condition.ClassId = new ParameterValue<int?>() { V = 8065, O = OptEnum.Equal };
                List<QueryPersonInfoCondition> list = new List<QueryPersonInfoCondition>();
                list.Add(condition);
                int empNum = personServiceClient.GetExpertsCount(JsonConvert.SerializeObject(list), string.Empty);
                if (empNum > 0)
                {
                    verifyIdCard = true;
                }
                if (verifyIdCard == true)
                {
                    retIdCard = -2;
                }
                else
                {
                    retIdCard = 2;
                }
                string output = JsonConvert.SerializeObject(new { status1 = retMobileNum, status2 = retIdCard });
                return output;
            }

        }
        #endregion
    }
}